package com.aifund.app.service.impl;

import com.aifund.app.entity.vo.home.HomeDataVo;
import com.aifund.app.entity.vo.home.MessageVo;
import com.aifund.app.entity.vo.home.ProjectVo;
import com.aifund.app.entity.vo.home.VideoVo;
import com.aifund.app.service.HomeService;
import com.aifund.app.service.SystemMsgService;
import com.aifund.app.service.TutorialVideoService;
import com.aifund.common.entity.SystemConfig;
import com.aifund.common.entity.SystemMsg;
import com.aifund.common.entity.TutorialVideo;
import com.aifund.common.enums.config.ConfigEnum;
import com.aifund.common.exception.AiFundException;
import com.aifund.common.result.ResultBody;
import com.aifund.common.service.SystemConfigService;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author deng
 */
@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    SystemConfigService systemConfigService;
    @Autowired
    SystemMsgService systemMsgService;
    @Autowired
    TutorialVideoService tutorialVideoService;

    @Override
    public ResultBody<HomeDataVo> getHomeData(Long userId) {
        HomeDataVo homeDataVo = new HomeDataVo();

        /**
         * 系统消息
         */
        List<SystemMsg> all = systemMsgService.findAll();
        SystemMsg systemMsg = null;
        if(all.size()>0){
            systemMsg = all.get(0);
        }else{
            systemMsg = new SystemMsg();
        }
        MessageVo messageVo = new MessageVo();
        BeanUtils.copyProperties(systemMsg,messageVo);


        /**
         * 项目栏
         */
        List<ProjectVo> projectVos = new ArrayList<>();

        /**
         * A股
         */
        SystemConfig aShareConfig = systemConfigService.getByKey(ConfigEnum.A_SHARES.getConfigKey());
        ProjectVo aShares = new ProjectVo();
        aShares.setTitle(aShareConfig.getConfigName());
        aShares.setProUrl(aShareConfig.getConfigValue());
        projectVos.add(aShares);

        /**
         * 港美
         */
        SystemConfig hongkongConfig = systemConfigService.getByKey(ConfigEnum.A_SHARES.getConfigKey());
        ProjectVo hongkongVp = new ProjectVo();
        hongkongVp.setTitle(hongkongConfig.getConfigName());
        hongkongVp.setProUrl(hongkongConfig.getConfigValue());
        projectVos.add(hongkongVp);

        /**
         * 开户
         */
        SystemConfig openAccountConfig = systemConfigService.getByKey(ConfigEnum.OPEN_ACCOUNT.getConfigKey());
        ProjectVo openAccount = new ProjectVo();
        openAccount.setTitle(openAccountConfig.getConfigName());
        openAccount.setProUrl(openAccountConfig.getConfigValue());
        projectVos.add(openAccount);

        /**
         * 基金
         */
        SystemConfig fundConfig = systemConfigService.getByKey(ConfigEnum.FUND_URL.getConfigKey());
        ProjectVo fund = new ProjectVo();
        fund.setTitle(fundConfig.getConfigName());
        fund.setProUrl(fundConfig.getConfigValue());
        projectVos.add(fund);

        /**
         * 期货
         */
        SystemConfig futuresConfig = systemConfigService.getByKey(ConfigEnum.FUTURES.getConfigKey());
        ProjectVo future = new ProjectVo();
        future.setTitle(futuresConfig.getConfigName());
        future.setProUrl(futuresConfig.getConfigValue());
        projectVos.add(future);

        homeDataVo.setProjectVo(projectVos);
        homeDataVo.setMsg(messageVo);

        /**
         * 视频教程
         */
        List<TutorialVideo> tutors = tutorialVideoService.getTutors(4);
        List<VideoVo> videoVos = new ArrayList<>();
        for(TutorialVideo tutorialVideo : tutors){
            VideoVo videoVo = new VideoVo();
            BeanUtils.copyProperties(tutorialVideo,videoVo);
            videoVos.add(videoVo);
        }
        homeDataVo.setVideoVos(videoVos);

        return ResultBody.success(homeDataVo);
    }



}
